﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

using SBPweb.RIA.Client.Controls.Extensions;

namespace SBPweb.RIA.Client.Controls.Content
{

    public partial class ToolBarItem : UserControl
    {

        #region Constructor logic

        public ToolBarItem()
        {
            InitializeComponent();
            this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ToolBarItem_IsEnabledChanged);
            CacheMode = new BitmapCache();
            CreateBindings();
        }

        #endregion

        #region Dependencies

        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToolBarItem), null);
        public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(ToolBarItem), null);

        #endregion

        #region Property accessors

        public Stretch Stretch
        {
            get
            {
                return image.Stretch;
            }
            set
            {
                image.Stretch = value;
            }
        }

        public ImageSource ImageSource
        {
            get
            {
                return GetValue(ImageSourceProperty) as ImageSource;
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

        public string Caption
        {
            get
            {
                return GetValue(CaptionProperty) as string;
            }
            set
            {
                SetValue(CaptionProperty, value);
            }
        }

        public bool IsCaptionVisible
        {
            get
            {
                return caption.Visibility == Visibility.Visible;
            }
            set
            {
                if (value)
                {
                    caption.Visibility = Visibility.Visible;
                }
                else
                {
                    caption.Visibility = Visibility.Collapsed;
                }
            }
        }

        public double? OpacityNormal
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnter).From;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnter).From = value;
                GetAnimationByStoryboard(storyMouseLeave).To = value;
                GetAnimationByStoryboard(storyEnableChanged).To = value;
                GetAnimationByStoryboard(storyEnableChangedDisabled).From = value;
            }
        }

        public double? OpacityDisabled
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnterDisabled).From;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnterDisabled).From = value;
                GetAnimationByStoryboard(storyMouseLeaveDisabled).To = value;
                GetAnimationByStoryboard(storyEnableChanged).From = value;
                GetAnimationByStoryboard(storyEnableChangedDisabled).To = value;
            }
        }

        public double? OpacityHover
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnter).To;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnter).To = value;
                GetAnimationByStoryboard(storyMouseLeave).From = value;
            }
        }

        public double? OpacityHoverDisabled
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnterDisabled).To;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnterDisabled).To = value;
                GetAnimationByStoryboard(storyMouseLeaveDisabled).From = value;
            }
        }

        #endregion

        #region Event handlers

        private void Element_MouseEnter(object sender, MouseEventArgs e)
        {
            this.StartStory(sender as FrameworkElement, "storyMouseEnter");
        }

        private void Element_MouseLeave(object sender, MouseEventArgs e)
        {
            this.StartStory(sender as FrameworkElement, "storyMouseLeave");
        }

        private void ToolBarItem_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.StartStory(sender as FrameworkElement, "storyEnableChanged");
        }

        #endregion

        #region Methods

        private DoubleAnimation GetAnimationByStoryboard(Storyboard sb)
        {
            return sb.Children[0] as DoubleAnimation;
        }

        private void CreateBindings()
        {
            Binding bndImageSource = new Binding();
            bndImageSource.Mode = BindingMode.TwoWay;
            bndImageSource.Source = this;
            bndImageSource.Path = new PropertyPath("ImageSource");
            this.image.SetBinding(Image.SourceProperty, bndImageSource);

            Binding bndCaption = new Binding();
            bndCaption.Mode = BindingMode.TwoWay;
            bndCaption.Source = this;
            bndCaption.Path = new PropertyPath("Caption");
            this.caption.SetBinding(TextBlock.TextProperty, bndCaption);
        }

        #endregion

    }

}